package com.hhdys.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhdys.dao.DepartmentMapper;
import com.hhdys.model.Department;

@Service
public class DepartmentServiceImpl implements DepartmentService {
	@Autowired
	private DepartmentMapper dao;

	@Override
	public List<Department> showDepartments() {
		return dao.selectByExample(null);
	}

	private String preDepartJson(List<Department> list, int parentId) {
		StringBuilder builder = new StringBuilder();
		for (Department dep : list) {
			if (dep.getParentId() == parentId) {
				builder.append("{\"name\":\"" + dep.getName()
						+ "\",\"title\":\"" + dep.getName() + "\",\"id\":"
						+ dep.getId()
						+ ",\"open\":\"true\",\"click\":\"nodeClick("
						+ dep.getId() + ",'" + dep.getName() + "',"
						+ dep.getParentId() + ")\",\"children\":["
						+ preDepartJson(list, dep.getId()) + "]},");
			}
		}
		return builder.toString();
	}

	/**
	 * @return the dao
	 */
	public DepartmentMapper getDao() {
		return dao;
	}

	/**
	 * @param dao
	 *            the dao to set
	 */
	public void setDao(DepartmentMapper dao) {
		this.dao = dao;
	}

	@Override
	public String getDepartmentsJson() {
		List<Department> list = dao.selectByExample(null);
		StringBuilder builder = new StringBuilder();
		builder.append("[");
		builder.append(preDepartJson(list, 0));
		builder.append("]");
		return builder.toString();
	}

	@Override
	public boolean addDeparment(int parentId, String name, String addUser) {
		Department department = new Department();
		department.setLastModifier(addUser);
		department.setLastModifyTime(new Date());
		department.setName(name);
		department.setParentId(parentId);
		return dao.insert(department) > 0;
	}

	@Override
	public boolean delDepartment(int id) {
		return dao.deleteByPrimaryKey(id) > 0;
	}

	@Override
	public boolean EditDepartment(int id, String name, String modifyUser) {
		Department department = new Department();
		department.setId(id);
		department.setLastModifier(modifyUser);
		department.setLastModifyTime(new Date());
		department.setName(name);
		return dao.updateByPrimaryKeySelective(department) > 0;
	}

	@Override
	public String showDepartmentsJson() {
		List<Department> list = showDepartments();
		StringBuilder builder = new StringBuilder();
		builder.append("[");
		builder.append(showDepartJson(list, 0));
		builder.append("]");
		return builder.toString();
	}

	private String showDepartJson(List<Department> list, int parentId) {
		StringBuilder builder = new StringBuilder();
		for (Department dep : list) {
			if (dep.getParentId() == parentId) {
				builder.append("{\"name\":\"" + dep.getName()
						+ "\",\"title\":\"" + dep.getName() + "\",\"id\":"
						+ dep.getId() + ",\"open\":\"true\",\"children\":["
						+ showDepartJson(list, dep.getId()) + "]},");
			}
		}
		return builder.toString();
	}

	@Override
	public Department getDepartmentById(int id) {
		return dao.selectByPrimaryKey(id);
	}
}
